<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加商品分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__PUBLIC__/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加商品分类</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group" id="myform" action="javascript:;">
                <div class="layui-form-item">
                    <label class="layui-form-label">栏目名</label>
                    <div class="layui-input-block">
                        <input type="text" name="ca_name" lay-verify="title" autocomplete="off" placeholder="请输入栏目名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="ca_sort" autocomplete="off" placeholder="请不要乱输入哦" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">所属分类</label>
                        <div class="layui-input-inline" style="z-index:1000">
                            <select name="ca_parent" lay-verify="required" lay-search="">
                                <option value="1">特价</option>
                                <option value="2">抢先兑</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1" type="button">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="__PUBLIC__/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="__PUBLIC__/caidongweb/lib/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__PUBLIC__/caidongweb/lib/ueditor/ueditor.all.js"> </script>
<script type="text/javascript">
    var ue = UE.getEditor('container');
</script>
<script>
    layui.config({
        base: '__PUBLIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });

        /* 自定义验证规则 */
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '标题至少得1个字符啊';
                }
                if(value.length > 100){
                    return '标题字数不得超出100';
                }
            }
        });

        /* 监听提交 */
        form.on('submit(component-form-demo1)', function(data){
            //获取相关的值
            $.ajax({
                type:'post',
                url:'{:url("Goods/cate_add")}',
                data:$("#myform").serialize(),
                dataType:'json',
                success: function (data) {
                    if(data.code>0){
                        //上传成功
                        layer.msg(data.msg,{icon:6,time:1500});
                        setTimeout(function(){parent.location.reload()},1500);
                    }else{
                        layer.msg(data.msg, {icon: 5, time: 1000});
                    }
                },
                error: function (data) {
                    console.log(data.msg);
                },
            });
        });
    });

    //封面上传
    layui.use('upload', function() {
        var $ = layui.jquery
            , upload = layui.upload;

        var id = $("[name=id]").val();
        var url = '{:url("Goods/uploads")}';
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: url
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if(res.code < 0){
                    return layer.msg(res.msg);
                }else{
                    //上传成功
                    layer.msg(res.msg);
                    $("[name=headImg]").val(res.src);
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>
</html>
